<?php
/**
 * 产品模型管理
 * @author cjli
 *
 */ 
class ProductModel extends Model
{
	/**
	 * 获得商品货号
	 * @param int $pid 产品ID号
	 * @return String
	 */
	public static function getProductSn($pid)
	{
		$total_len = 12;
		$sn_pre = C('product_sn_pre');
		/**
		if(!empty($sn_pre))
	 	{
	 		if(strlen($sn_pre)>2)
	 		{
	 			$sn_pre = substr($sn_pre,0,2);
	 		}
	 		else if(strlen($sn_pre)==1)
	 		{
	 			$sn_pre = $sn_pre."0";
	 		}
	 	}
	 	else
	 	{
	 		$sn_pre = 'SN';
	 	}
	 	**/
		if(empty($sn_pre)){
		$sn_pre = 'SN';
		}
		
	 	$sn_len = strlen($sn_pre);
	 	
	 	//判断加0的个数
		if(($total_len-$sn_len-strlen($pid))>0)
		{
			$j = $total_len-$sn_len-strlen($pid);
			for ($i = 0; $i < $j; $i++) {
				$sn_pre = $sn_pre."0";
			}
		}
	 	//组合货号
	 	$sn_pre = $sn_pre.$pid;
	 	return $sn_pre;
	}
	
	/**
	 * 生成产品唯一标识
	 * @param array post数据
	 * @return string
	 */
	public static function getProductMarking($post)
	{
		return md5(json_encode($post));
	}
	
	/**
	 * 获取产品信息
	 * @param int $pid 产品ID号
	 * @return array
	 */
	public static function getProductInfo($pid)
	{
		$product_db = D('product');
		$info = $product_db->find($pid);
		if($info['restriction'] > 0)
			$info['style'] = 'display:block;';
		else
			$info['style'] = 'display:none;';
		return $info;
	}
	
	/**
	 * 删除产品缓存
	 * @paramint $pid 产品ID号
	 */
	public static function deleteProductCache($pid)
	{
		$languageList = SiteModel::getLanguageList();
		foreach($languageList as $language_id => $language_name){
			$cacheId = 'product_'.$pid.'_'.$language_id;
			S($cacheId, NULL);
		}
		S('wholesale_list', NULL);
	}
	
	/**
	 * 更新产品信息
	 * @param array $post post数据
	 * @param int $pid 产品ID号
	 */
	public static function updateProductInfo($post, $pid = null,$did=0)
	{
		$product_db = D('product');
		if($post['xianzhi'] > 0){
			$restriction = intval($post['restriction']);
		}else{
			$restriction = 0;
		}
		$post['stock'] = $post['stock'];

		//非预售时定金为零
		if($post['product_type'] > 0){
			$yufu_price = floatval($post['yufu_price']);
		}else{
			$yufu_price = floatval(0);
		}		
		$data = array(
			'catid' => intval($post['catid']),
			'model_id' => intval($post['model_id']),
			'brand_id' => intval($post['brand_id']),
			'display' => intval($post['display']),
			'restriction' => $restriction,
			'sell_price' => floatval($post['sell_price']),
			'custom_price' => floatval($post['custom_price']),
			'advanced_price' => floatval($post['advanced_price']),
			'original_price' =>floatval($post['original_price']),
			'market_price' => floatval($post['market_price']),
			'cost_price' => floatval($post['cost_price']),
			'yufu_price' => $yufu_price,
			'weight' => floatval($post['weight']),
			'stock' => intval($post['stock']),
			'img' => trim($post['img']),
			'small_img' => trim($post['small_img']),
			'list_img' => trim($post['list_img']),
			'bg_img' => isset($post['bg_img']) ? trim($post['bg_img']) : '',
			'path_alias' => trim($post['path_alias']),
			'template' => trim($post['template']),
			'visit' => intval($post['visit']),
			'ml_img'=>trim($post['ml_img']),
			'goods_img'=>trim($post['goods_img']),
			'new_product'=>trim($post['new_product']),
			'hot'=>trim($post['hot']),
			'special_process' =>trim($post['special_process']),
			'end_time' => time(),
			'yushou_start_time' => strtotime(trim($post['yushou_start_time'])),
			'yushou_end_time' => strtotime(trim($post['yushou_end_time'])),
			'weikuan_time' => strtotime(trim($post['weikuan_time'])),
			'listorder' => intval($post['listorder']),
			'rank_integral' => (isset($post['rank_integral']) && $post['rank_integral']!=-1) ? intval($post['rank_integral']) : -1,
			'integral' => (isset($post['integral']) && $post['integral']!=-1) ? intval($post['integral']) : -1,
			'suppliers_id' => intval($post['suppliers_id']),
			'is_delete' => 0,
			'issend' => 0,
			'designer_id' => isset($post['designer_id']) ? intval($post['designer_id']) : 0,
			'designer_product_id' => isset($post['designer_product_id']) ? intval($post['designer_product_id']) : 0,
			'is_sell' => isset($post['is_sell']) ? $post['is_sell'] : 0,
			'yushou_display' => isset($post['yushou_display']) ? $post['yushou_display'] : 0,
			'product_type' => isset($post['product_type']) ? $post['product_type'] : 0,
			'attribute_type' => isset($post['attribute_type']) ? $post['attribute_type'] : 0,
			'refund' => isset($post['refund']) ? $post['refund'] : 0,
			'category_id'  => !empty($post['categoryId'])  ? implode(',', array_unique(array_diff($post['categoryId'],array(0)))) : 0,
			'colour_id'  => !empty($post['colourId'])  ? implode(',', array_unique(array_diff($post['colourId'],array(0)))) : 0,
			'two_suit_sn' => $post['two_suit_sn'],
			'add_more'    => $post['add_more'],
			'pants_price' => floatval($post['pants_price']) > 0 ? floatval($post['pants_price']) : 0,
		    'button_name' => $post['button_name'],
		    'product_colour_id'  => !empty($post['product_colourId'])  ? implode(',', array_unique(array_diff($post['product_colourId'],array(0)))) : 0,
            'shoes_size_min' => $post['shoes_size_min']?floatval($post['shoes_size_min']):null,
            'shoes_size_max' => $post['shoes_size_max']?floatval($post['shoes_size_max']):null,
            'shoes_size_del' => $post['shoes_size_del']?$post['shoes_size_del']:null,
            'manufacture' => $post['manufacture']?$post['manufacture']:null,
            'model_no' => $post['model_no']?$post['model_no']:null,
            'eng_id' => $post['eng_id']?$post['eng_id']:null,
			'show_attr_ids'=>$post['show_attr_ids']?$post['show_attr_ids']:"",
		);
		if($did){
			$data['start_time'] = time();
			$data['product_marking'] = self::getProductMarking($data);
			$pid = $product_db->add($data);
		}else{
			if($pid){
				$product_db->where(array('id' => $pid))->save($data);
			}else{
				$data['start_time'] = time();
				$data['product_marking'] = self::getProductMarking($data);
				$pid = $product_db->add($data);
			}
		}
		
		//更新货号
		$sn = trim($post['sn']);
		if(empty($sn)){
			$sn = self::getProductSn($pid);
		}else{
			$count = $product_db->where(array('sn' => $sn, 'id' => array('neq',$pid)))->count();
			if($count){
				$sn .= '-'.$pid;
			}
		}
		$product_db->where(array('id' => $pid))->save(array('sn' => $sn));
		//删除缓存
		self::deleteProductCache($pid);
		return $pid;
	}
	
	/**
	 * 更新产品内容详细
	 * @param array $post post数据
	 * @param int $pid 产品ID号
	 */
	public static function updateProductItems($post, $pid)
	{
		$productInfo = self::getProductInfo($pid);
		if(!$productInfo) return false;
		$product_items_db = D('product_items');
		$product_items_db->where(array('product_id' => $pid))->delete();
		
		$languageList = SiteModel::getLanguageList();
		foreach($languageList as $language_id => $language_name){
			if($post[$language_id]['name']){
				$data = array(
					'product_id' => $pid,
					'language_id' => $language_id,
					'name' => trim($post[$language_id]['name']),
					'summary' => trim($post[$language_id]['summary']),
					'content' => trim($post[$language_id]['content']),
					'description' => $post[$language_id]['description'],
					'meta_title' => trim($post[$language_id]['meta_title']),
					'meta_keywords' => trim($post[$language_id]['meta_keywords']),
					'meta_description' => trim($post[$language_id]['meta_description']),
				);
				$product_items_db->add($data);
			}
		}
	}
	
	/**
	 * 获取产品内容详细
	 * @param int $pid 产品ID
	 * @param int $language_id 语言ID号
	 */
	public static function getProductItems($pid, $language_id = 0)
	{
		$where['product_id'] = $pid;
		if($language_id){
			$where['language_id'] = $language_id;
		}
		$productItemsArray = array();
		$product_items_db = D('product_items');
		$productItems = $product_items_db->where($where)->select();
		if($productItems){
			foreach ($productItems as $item){
				$productItemsArray[$item['language_id']] = $item;
			}
		}
		return $productItemsArray;
	}
	
	/**
	 * 删除产品信息
	 * @param int $pid 产品ID
	 * @return vod
	 */
	public static function deleteProductInfo($pid)
	{
		$product_db = D('product');
		$product_db->delete($pid);
		
		//删除产品详细
		$product_items_db = D('product_items');
		$product_items_db->where(array('product_id' => $pid))->delete();
		
		
		//删除产品关联分类
		self::deleteProductCategoryRelation($pid);
		
		//删除产品关联图片
		self::deleteProductPhotoRelation($pid);
		
		//删除产品关联属性
		self::deleteProductAttributeRelation($pid);
		
		//删除批发关联
		PromotionModel::deleteProductWholesaleInfo($pid);
		
		//删除商品关联
		$product_relation_db = D('product_relations');
		$product_relation_db->delete($pid);
		
		//删除缓存
		self::deleteProductCache($pid);
	}
	
	/**
	 * 删除产品关联分类
	 * @param int $pid 产品ID
	 */
	public static function deleteProductCategoryRelation($pid)
	{
		$category_product_db = D('category_product');
		$category_product_db->where('pid='.$pid)->delete();
	}
	
	/**
	 * 删除产品关联图片
	 * @param int $pid 产品ID号
	 */
	public static function deleteProductPhotoRelation($pid)
	{
		//TODO没有删除图片
		$product_photo_relation_db = D('product_photo_relation');
		$product_photo_relation_db->where('pid='.$pid)->delete();
	}
	
	/**
	 * 删除产品关联属性
	 * @param int $pid 产品ID号
	 */
	public static function deleteProductAttributeRelation($pid)
	{
		$product_attribute_db = D('product_attribute');
		$product_attribute_db->where('product_id='.$pid)->delete();
	}
	
	 /**
   * 获取相关商品信息
   * @param int $pid 商品ID
   * @param int $firstRow 从第几个起
   * @param int $pageRows 一页多少个
   */
  public static function getRelatedProductList($pid, $firstRow = 0, $pageRows = 20)
  {
  	$productRelationList = array();
  	$language_id = C('language_id');
    $product_relation_db = D('product_relations');
    $product_relation_db->field('related_pid AS pid,product_items.name');
    $product_relation_db->join('product_items ON product_items.product_id = product_relations.related_pid');
    $product_relation_db->where(array('product_relations.pid'=>$pid, 'product_items.language_id' => $language_id));
    $productRelationList = $product_relation_db->limit($firstRow, $pageRows)->select();
    if($productRelationList){
	  	foreach ($productRelationList as $key => $relation) {
	    $count = $product_relation_db->where(array('pid' => $relation['pid'], 'related_pid' => $pid))->count();
	    	$productRelationList[$key]['isbothway'] = $count ? 1 : 0;
	     }
    }
    return $productRelationList;
  }

  /**
   *
   * 获取相关商品总数
   * @param int $pid
   */
  public static function getRelatedProductCount($pid)
  {
   	$product_relation_db = D('product_relations');
    $product_relation_db->join('product ON product.id = product_relations.related_pid');
    $product_relation_db->where(array('product_relations.pid'=>$pid));
    $num = $product_relation_db->count();
    return $num;
  }

  /**
   *
   * 删除相关的商品信息
   * @param int $pid
   * @param int $related_pid
   */
  public static function deleteProductRelated($pid, $related_pid)
  {
  	$product_relation_db = D('product_relations');
    $where = array(
      'pid' => $pid,
      'related_pid' => $related_pid,
    );
    $product_relation_db->where($where)->delete();
  }

  /**
   *
   * 更新相关商品信息
   * @param int $pid
   * @param array $post
   */
  public static function updateProductRelated($pid, $post)
  {
    $product_relation_db = D('product_relations');
    $product_relation_db->where(array('pid' => $pid))->delete();
    if($post){
	    foreach ($post as $dl){
	      $set = array(
	        'pid' => $pid,
	        'related_pid' => $dl->pid,
	      );
	      $product_relation_db->add($set);
	      
	      //反向关联
	      $rset = array(
	         'pid' => $dl->pid,
	         'related_pid' => $pid,
	        );
	      if ($dl->isbothway == 1) {
	        $count = $product_relation_db->where($rset)->count();
	        if(!$count){
	          $product_relation_db->add($rset);
	        }
	      } else {
	        $product_relation_db->where($rset)->delete();
	      }
	    }
    }
  }
	
	
	/**
	 * 产品关联图片
	 * @param int $pid 产品ID
	 */
	public static function getProductPhotoList($pid,$did=0)
	{
		$product_photo_db = D('product_photo');
		if($did>0){
			$files = $product_photo_db->field('product_photo.*')->join('product_photo_relation AS pr ON pr.fid = product_photo.fid')->where('pr.did='.$did)->order('product_photo.order ASC,product_photo.fid ASC')->select();
		}else{
			$files = $product_photo_db->field('product_photo.*')->join('product_photo_relation AS pr ON pr.fid = product_photo.fid')->where('pr.pid='.$pid)->order('product_photo.order ASC,product_photo.fid ASC')->select();
		}
		foreach ($files as &$file){
			if(empty($file['thumb'])){
				$file['thumb'] = dirname($file['img']).'/thumb_'.basename($file['img']);
			}
		}
		return $files;
	}

	/**
	 * 产品关联图片
	 * @param int $pid 产品ID
	 */
	public static function getProductPhotoBackgroundList($pid)
	{
		$product_photo_db = D('product_background_photo');
        $files = $product_photo_db->where('pid='.$pid)->select();
		return $files;
	}
	
	/**
	 * 获取产品关联属性列表
	 * @param int $pid 产品ID号
	 * @return array
	 */
	public static function getProductRelationAttribteList($pid)
	{
		$pid = intval($pid);
		if(!$pid){
			return array();
		}

		$product_attribute_db = D('product_attribute');
		$sql = "SELECT a.attr_id, a.attr_name, g.product_attr_id, g.attr_value, g.attr_price, a.attr_input_type, a.attr_type, av.attr_value AS attr_value_value " .
                "FROM product_attribute AS g, attribute  AS a , attribute_values AS av " .
                "WHERE g.attr_id = a.attr_id AND g.attr_value_id = av.id " .
                "AND g.product_id = '$pid' ";
        $attr_list = array();
        $productAttributeList = $product_attribute_db->query($sql);
        if($productAttributeList){
	        foreach ($productAttributeList as $pro_attr){
	        	$pro_attr['attr_value'] = $pro_attr['attr_value'] ? $pro_attr['attr_value'] : $pro_attr['attr_value_value'];
	         	$attr_list[$pro_attr['attr_id']][] = $pro_attr;
	        }
        }
        $attr_list = array_values($attr_list);
		return $attr_list;
	}
	
/**
	 * 获取商品最低价格
	 * @param int $pid 商品ID
	 * @param int $qty 商品数量
	 * @param int $rank_id 会员等级
	 */
	public static function getProductMinPrice($pid, $qty = 1, $rank_id = 0)
	{
		$low_price = 0;
		$product = self::getProductInfo($pid);
		if($product)
		{
			//销售价格
			$low_price = $product['sell_price'];
			
			//批发价格
			$wholesale_price = PromotionModel::getProductWholesalePrice($pid, $qty, intval($rank_id));
			if ($wholesale_price)
			{
				$low_price = min(array($wholesale_price, $low_price));
			}
		}
		return $low_price;
	}
	
	/**
	 * 更新产品库存
	 * @param int $pid 产品ID号
	 * @param int $num 数量
	 */
	public static function updateStock($pid, $num)
	{
		$product_db = D('product');
		$product_db->where('id='.$pid)->setInc('stock', $num);
	}
	
	/**
	 * 获取商品库存
	 * @param int $pid 商品ID号
	 */
	public static function getProductStock($pid)
	{
		$product_db = D('product');
		$stock = $product_db->where( array('id' => $pid) )->getField('stock');
		return $stock;
	}	
	
	/**
	 * 获取扩展分类
	 * @param int $pid 商品ID号
	 */
	public static function getOtherCatids($pid)
	{
		$catIds = array();
		$cat_db = D('product_other_cat');
		$catIdsArray = $cat_db->field('catid')->where(array('pid' => $pid))->select();
		if($catIdsArray){
			foreach ($catIdsArray as $cat){
				$catIds[] = $cat['catid'];
			}
		}
		return $catIds;
	}
	
	/**
	 * 获取设计师作品详细
	 * @param int $designer_product_id
	 */
	public static function getUserDesignerProductInfo($designer_product_id)
	{
		$user_designer_product = D('user_designer_product');
		$info = $user_designer_product->where('designer_product_id='.$designer_product_id)->find();
		return $info;
	}
	
	/**
	 * 更新设计师作品详细
	 * @param array $post
	 * @param int $desginer_product_id
	 */
	public static function updateUserDesignerProductInfo($post, $desginer_product_id)
	{
		$user_designer_product = D('user_designer_product');
		$data = array(
			'model_id' => intval($post['model_id']),
			'display' => intval($post['display']),
			'img' => trim($post['img']),
			'small_img' => trim($post['small_img']),
			'list_img' => trim($post['list_img']),
			'bg_img' => isset($post['bg_img']) ? trim($post['bg_img']) : '',
			'end_time' => time(),
			'designer_id' => isset($post['designer_id']) ? intval($post['designer_id']) : 0,
			'product_up' => isset($post['product_up']) ? intval($post['product_up']) : 0,
			'product_down' => isset($post['product_down']) ? intval($post['product_down']) : 0,
			'designer_name' => trim($post['designer_name']),
			'designer_description' => trim($post['designer_description']),
			'attr' => $post['attr'] ? serialize($post['attr']) : null,
		);
		if($desginer_product_id){
			$user_designer_product->where('designer_product_id='.$desginer_product_id)->save($data);
		}else{
			$data['start_time'] = time();
			$desginer_product_id = $user_designer_product->add($data);
		}
		return $desginer_product_id;
	}
	
	/**
	 * 作品关联图片
	 * @param int $desginer_product_id 作品ID
	 */
	public static function getUserDesignerProductPhotoList($desginer_product_id)
	{
		$product_photo_db = D('product_photo');
		$files = $product_photo_db->field('product_photo.*')->join('user_designer_photo_relation AS pr ON pr.fid = product_photo.fid')->where('pr.pid='.$desginer_product_id)->select();
		foreach ($files as &$file){
			$file['thumb'] = dirname($file['img']).'/thumb_'.basename($file['img']);
		}
		unset($file);
		return $files;
	}
	
	public static function getDesignerProductSelectList($designer_id)
	{
		$where = array(
			'display' => 1,
			'designer_id' => $designer_id,
		);
		$user_designer_product = D('user_designer_product');
		$user_designer_product->field('designer_product_id ,designer_name');
		$user_designer_product->where($where);
		$productList = $user_designer_product->select();
		return $productList;
	}
	
	/**
	 * 晒单关联图片
	 * @param int $pid 产品ID
	 */
	public static function getShaidanPhotoList($pid)
	{	
		//print_r($pid);
		$product_photo_db = D('product_photo');
		$files = $product_photo_db->field('product_photo.*')->join('shaidan_photo_relation AS pr ON pr.fid = product_photo.fid')->where('pr.sid='.$pid)->select();
		foreach ($files as &$file){
			$file['thumb'] = dirname($file['img']).'/thumb_'.basename($file['img']);
		}
		return $files;
	}
	
	/**
	* 获取商品信息
	* @param int $pid 商品ID号
	* @return array
	*/
	public static function get_product_info($pid, $language_id = 1)
	{
		$product_db = D('product');
		$where = array(
		    'language_id' => $language_id,
		    'is_delete' => 0,
		    'id' => $pid,
		);
		$product_db->join('product_items ON product_items.product_id = product.id');
		$info = $product_db->where($where)->find();

		$where_attr = array('product_id'=>$pid,'language_id'=>$language_id);
		$product_items_info = D('product_items')->where($where_attr)->find();
		$info['name'] = $product_items_info['name'];
		$info['url'] = '/product/'.$pid.'-'.generateUrl_un($product_items_info['name']);
		//中文名字
		$where_attr = array('product_id'=>$pid,'language_id'=>6);
		$product_items_info_cn = D('product_items')->where($where_attr)->find();
		$info['name_cn'] = $product_items_info_cn['name'];

		return $info;
	}	
	
	public static function get_product_custom_attribte_list($pid, $filter = array(),$language_id=1)
	{
		$pid = intval($pid);
		if(!$pid){
			return false;
		}
		$where = array(
			'pa.product_id' => $pid,
			'attribute_items.language_id' => $language_id,
		);
		if($filter){
			foreach ($filter as $key => $val){
				if(! is_null($val)){
					$where[$key] = $val;
				}
			}
		}

		/*if($custom_id){
			$where = 'pa.product_id = '.$pid.' and attr_sell = '.$where['attr_sell'].' and (attr_advanced = '.$where['attr_advanced'].' or attr_advanced = 2)';
		}*/
		//$where = 'pa.product_id = '.$pid.' and attr_sell = '.$where['attr_sell'].'';

		$attribute_db = D('attribute');

		$attribute_db->field('attribute.sort_order as attr_sort_order,av.sort_order as av_sort_order,attribute.attr_custom,attribute.attr_pro_sort,attribute.attr_pro_type,attribute.attr_pro_fid,attribute.size_type,attribute.attr_id,attribute.attr_sell,attribute.attr_group,attribute.attr_type,attribute.attr_input_type,attribute.issd as attr_issd,pa.attr_value_id, pa.attr_value,pa.attr_price,pa.product_attr_id,av.issd as attr_value_issd,av.attr_img,av.attr_img2,av.libu_img,av.add_money,pa.is_default,attribute_items.*');
		$attribute_db->join('product_attribute AS pa ON attribute.attr_id=pa.attr_id');
		$attribute_db->join('attribute_values AS av ON av.id = pa.attr_value_id');
		$attribute_db->join('attribute_items ON attribute_items.attribute_id = attribute.attr_id');
		$attribute_db->where($where);
		//$attribute_db->order('attribute.attr_id,pa.product_attr_id');
		$attribute_db->order('attribute.attr_custom DESC,attribute.sort_order ASC, av.sort_order ASC, attribute.attr_id ASC');
		$result = $attribute_db->select();
		//echo $attribute_db->getLastSql();exit;

		if($result){
			$attributes = array();
            
            $option_default = '';
			foreach ($result as $attr)
			{
				$is_default = 0;
				if($attr['is_default']==$attr['attr_value_id']){
					$is_default = $attr['product_attr_id'];
				}
				if($attr['attr_value_id']){
					$attribute_values_items_db = D('attribute_values_items');
					$attribute_values_items = $attribute_values_items_db->where(array('att_value_id' => $attr['attr_value_id'],'language_id' => $language_id))->find();
					$attr['attr_value'] = $attribute_values_items['attr_value'];
					$attr['attr_name_value'] = $attribute_values_items['attr_value'];
					$attr['attr_content'] = $attribute_values_items['attr_content'];

				}

				if($attr['attr_value_id'] && empty($attr['attr_value'])){
					$attr['attr_value'] = $attr['attr_name_value'];
				}

				$arr[$attr['attr_id']]['attr_id'] = $attr['attr_id'];
				$arr[$attr['attr_id']]['name']     = $attr['name'];
				//$arr[$attr['attr_id']]['attr_type']     = $attr['attr_type'];
				$arr[$attr['attr_id']]['attr_content']     = $attr['attr_content'];
			    //$arr[$attr['attr_id']]['attr_input_type'] = $attr['attr_input_type'];
				//$arr[$attr['attr_id']]['sort_order'] = $attr['attr_sort_order'];
				$arr[$attr['attr_id']]['attr_pro_type'] = $attr['attr_pro_type'];
				$arr[$attr['attr_id']]['attr_pro_fid'] = $attr['attr_pro_fid'];
				$arr[$attr['attr_id']]['attr_pro_sort'] = $attr['attr_pro_sort'];
				//$arr[$attr['attr_id']]['attr_help'] = AttributeModel::attribute_info($attr['attr_id']);
				$arr[$attr['attr_id']]['attr_custom'] = $attr['attr_custom'];
				$arr[$attr['attr_id']]['size_type'] = $attr['size_type'];
				$arr[$attr['attr_id']]['issd'] = $attr['attr_issd'];
				$arr[$attr['attr_id']]['values'][] = array(
															'label'        => $attr['attr_value'],
															'image'			=> $attr['attr_img'],
															'image2'		=> $attr['attr_img2'],
															'product_attr_id' => $attr['product_attr_id'],
															'attr_value_id' => $attr['attr_value_id'],
															'attr_content' => $attr['attr_content'],
															'sort_order' => $attr['av_sort_order']+100,
															'issd' => $attr['attr_value_issd'],
                                                            'libu_img'=>$attr['libu_img'],
                                                            'add_money'=>$attr['add_money'],
															);
				if($is_default){
					$arr[$attr['attr_id']]['default']     = $is_default;
					//给option里的attr_pro_type=3（颜色选项）使用,如default被清值后，用s_default做选中判断
					$arr[$attr['attr_id']]['s_default']   = $is_default;
				}
				//去掉绣字的默认值
				if($attr['attr_pro_type'] == 1||$attr['attr_pro_type'] == 10){
					$arr[$attr['attr_id']]['default'] = '';
				}
				
                if(in_array($attr['attr_id'], array(54,70,112,134))){
                	//option多选
                	if($attr['is_default']==$attr['attr_value_id']){
                	    $option_default .= $option_default ? '|'.$attr['product_attr_id'] : $attr['product_attr_id'];
                	}
                	
                	// $yjxk = array(668,783,791,744,800,497,667,784,792,745,801,498,741,807); //特殊需要开圆角袖口的商品id
            	    // if(in_array($pid, $yjxk)){        	    	
	            	   //  if(in_array($attr['attr_value_id'], array(762,765,761))){ //开圆角袖口
	                //         $option_default .= $option_default ? '|'.$attr['product_attr_id'] : $attr['product_attr_id'];
	            	   //  }             	               	    	
            	    // }

            	    // if(in_array($attr['attr_value_id'], array(133,136,210,340,342,642))){           	    	
	            	   //  if(in_array($pid, $yjxk)){
	            	   //  	if(!in_array($attr['attr_value_id'], array(136,342,210)))   //圆角袖口 和 Real Sleeve Slit 互斥
	            	   //          $option_default .= $option_default ? '|'.$attr['product_attr_id'] : $attr['product_attr_id'];     	           	               	    	
	            	   //  }else{
	            	   //      $option_default .= $option_default ? '|'.$attr['product_attr_id'] : $attr['product_attr_id']; 	            	    	
	            	   //  }                     
            	    // }
                    
                    // $kjfb = array(848,847);//裤脚翻边 商品id
                    // if(in_array($pid, $kjfb) && in_array($attr['attr_value_id'], array(131,338))){   
        	           //  $option_default .= $option_default ? '|'.$attr['product_attr_id'] : $attr['product_attr_id']; 	
                    // }
                    
                    // $pjd = array(877,878,834,835,836,838);//票据袋 商品id
                    // if(in_array($pid, $pjd) && in_array($attr['attr_value_id'], array(209,273,345))){   
        	           //  $option_default .= $option_default ? '|'.$attr['product_attr_id'] : $attr['product_attr_id']; 	
                    // }                    

            	    if($option_default) $arr[$attr['attr_id']]['default'] = $option_default;
                }

			}
		}
		return isset($arr) ? $arr : null;
	}
	
	public static function get_shoes_size_data($max_size,$min_size,$del_size=null){
        if(cache("get_shoes_size_data")){
            $show_data = cache("get_shoes_size_data");
        }else{
            $shoes_size_db=D("shoes_size_data");
            $shoes_size_db->order('cn_size');
            $shoes_size_data=$shoes_size_db->where('cn_size >='.$min_size." and cn_size<=".$max_size)->select();
            if($del_size&&$del_size>$min_size&&$del_size<$max_size){
                $del_size_arr=explode(",",$del_size);
            }
            foreach ($shoes_size_data as $k=>$v){
                if(!in_array($v['cn_size'],$del_size_arr)){
                    $show_data[]=$v['cn_size'];
                }
            }
            cache('get_shoes_size_data', $show_data);
            unset($shoes_size_data);
        }
        return  $show_data;
    }
}